package com.app.android.coliy.ui.view;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.android.coliy.R;
import com.app.android.coliy.adapter.ColorAdapter;
import com.app.android.coliy.manager.ColorManager;
import java.util.List;
import java.util.Random;

import razerdp.basepopup.BasePopupWindow;

public class ColorPopup extends BasePopupWindow {

    private Context mContext;

    private Callback mCallback;

    public interface Callback {

        void selectedColor(String color);

        void randomColors();
    }

    ColorPopup(Context context, Callback callback) {

        super(context);
        mContext = context;

        mCallback = callback;
    }


    @Override
    public View onCreateContentView() {

        View view = createPopupById(R.layout.layout_color_gravity);

        RecyclerView recyclerView = view.findViewById(R.id.color_recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        ColorAdapter mAdapter = new ColorAdapter();
        recyclerView.setAdapter(mAdapter);

        List<String> colors = ColorManager.getInstance().getColors();

        mAdapter.setNewData(colors);

        mAdapter.setOnItemClickListener((adapter, view1, position) -> {

            String color = (String) adapter.getItem(position);

            if (color != null) {
                if (mCallback != null) {
                    mCallback.selectedColor(color);
                }
            }
        });

        TextView randomColor = view.findViewById(R.id.tv_color_random);
        randomColor.setOnClickListener(v -> {
            if (mCallback != null) {
                mCallback.randomColors();
            }
        });


        StringBuilder stringBuilder = new StringBuilder();

        char[] texts = randomColor.getText().toString().toCharArray();

        for (char c:
                texts) {

            Random random = new Random();
            int number = random.nextInt(colors.size() - 1);
            String color = colors.get(number);

            String temp = "<font color='" + color + "'>" + c + "</font>";

            stringBuilder.append(temp);
        }

        randomColor.setText(Html.fromHtml(stringBuilder.toString()));


        return view;
    }
}
